#!/bin/sh

cd ../..
# path:./meeting

cd source
appName="meeting"

brands=$(ls "dist/brand")
for brand in ${brands}; do
	mkdir -p ../../../web-server/brand/${brand}/${appName}
	rm -rf ../../../web-server/brand/${brand}/${appName}/*
	s_brand="${brand}"
	if [ $(ls dist/brand/${brand} | wc -l) -eq 0 ]; then
		echo "${brand} not exists!!! try to use index."
		s_brand="index"
	fi
	\cp -a ./dist/brand/${s_brand}/* ../../../web-server/brand/${brand}/${appName}/
done
rm -rf dist/brand/*

# webServer-apps
rm -rf ../../../web-server/release/${appName}.bin
if [ -d "./dist" ]; then
	rm -rf ./release/*
	mkdir -p ./release
	tar -zcvf dist.tar.gz dist
	mv dist.tar.gz ./release/
	if [ -d "./debug" ]; then
		tar -zcvf debug.tar.gz debug
		mv debug.tar.gz ./release/
	fi
	echo "${appName}" >./release/appName
	\cp -av ../../../web-server/scripts/shell/web_install.sh ./release/
	# i18n begin
	if [ -d "./dist/i18n" ]; then
		cd ./dist
		mkdir -p ../release/i18n
		for locale in $(ls ./i18n); do
			mkdir -p ../release/i18n/${locale}/${appName}/i18n/
			if [ -f "../../../../web-server/i18n_langs/${locale}.ini" ]; then
				\cp -a ../../../../web-server/i18n_langs/${locale}.ini ../release/i18n/${locale}/lang.ini
			fi
			\cp -a ./i18n/${locale}/* ../release/i18n/${locale}/${appName}/i18n/
			echo "$(date) >> install ${locale} lang."
		done
		rm -rf ../../../../web-server/i18n/linux/*/${appName}/*
		rm -rf ../../../../web-server/i18n/linux_arm_ft64/*/${appName}/*
		mkdir -p ../../../../web-server/i18n/linux/
		mkdir -p ../../../../web-server/i18n/linux_arm_ft64/
		\cp -a ../release/i18n/* ../../../../web-server/i18n/linux/
		\cp -a ../release/i18n/* ../../../../web-server/i18n/linux_arm_ft64/
		cd -
	fi
	# i18n end
	makeself ./release ./${appName}.bin "Deploy the ${appName} application..." ./web_install.sh
	mkdir -p ../../../web-server/release/
	mv ./${appName}.bin ../../../web-server/release/

	for brand in ${brands}; do
		if [ "index" != "${brand}" ]; then
			rm -rf ./release/dist.tar.gz
			rm -rf ./release/web_install.sh
			rm -rf ./release/appName
			mkdir -p ./release
			rm -rf dist/brand/index/*
			mkdir -p dist/brand/index
			cp -a ../../../web-server/brand/${brand}/${appName}/* dist/brand/index/
			tar -zcvf dist.tar.gz dist
			mv dist.tar.gz ./release/
			if [ -d "./debug" ]; then
				tar -zcvf debug.tar.gz debug
				mv debug.tar.gz ./release/
			fi
			echo "${appName}" >./release/appName
			\cp -av ../../../web-server/scripts/shell/web_install.sh ./release/
			makeself ./release ./${appName}_${brand}.bin "Deploy the ${appName}_${brand} application..." ./web_install.sh
			mkdir -p ../../../web-server/release_brand/
			rm -rf ../../../web-server/release_brand/${appName}_${brand}.bin
			mv ./${appName}_${brand}.bin ../../../web-server/release_brand/
		fi
	done
fi
cd ..

cd source-sm
appName="meeting_sm"

brands=$(ls "dist/brand")
for brand in ${brands}; do
	mkdir -p ../../../web-server/brand/${brand}/${appName}
	rm -rf ../../../web-server/brand/${brand}/${appName}/*
	s_brand="${brand}"
	if [ $(ls dist/brand/${brand} | wc -l) -eq 0 ]; then
		echo "${brand} not exists!!! try to use index."
		s_brand="index"
	fi
	\cp -a ./dist/brand/${s_brand}/* ../../../web-server/brand/${brand}/${appName}/
done
rm -rf dist/brand/*

# webServer-apps
rm -rf ../../../web-server/release/${appName}.bin
if [ -d "./dist" ]; then
	rm -rf ./release/*
	mkdir -p ./release
	tar -zcvf dist.tar.gz dist
	mv dist.tar.gz ./release/
	if [ -d "./debug" ]; then
		tar -zcvf debug.tar.gz debug
		mv debug.tar.gz ./release/
	fi
	echo "${appName}" >./release/appName
	\cp -av ../../../web-server/scripts/shell/web_install.sh ./release/
	# i18n begin
	if [ -d "./dist/i18n" ]; then
		cd ./dist
		mkdir -p ../release/i18n
		for locale in $(ls ./i18n); do
			mkdir -p ../release/i18n/${locale}/${appName}/i18n/
			if [ -f "../../../../web-server/i18n_langs/${locale}.ini" ]; then
				\cp -a ../../../../web-server/i18n_langs/${locale}.ini ../release/i18n/${locale}/lang.ini
			fi
			\cp -a ./i18n/${locale}/* ../release/i18n/${locale}/${appName}/i18n/
			echo "$(date) >> install ${locale} lang."
		done
		rm -rf ../../../../web-server/i18n/linux_arm_ft64_sm/*/${appName}/*
		mkdir -p ../../../../web-server/i18n/linux_arm_ft64_sm/
		\cp -a ../release/i18n/* ../../../../web-server/i18n/linux_arm_ft64_sm/
		cd -
	fi
	# i18n end
	makeself ./release ./${appName}.bin "Deploy the ${appName} application..." ./web_install.sh
	mkdir -p ../../../web-server/release/
	mv ./${appName}.bin ../../../web-server/release/

	for brand in ${brands}; do
		if [ "index" != "${brand}" ]; then
			rm -rf ./release/dist.tar.gz
			rm -rf ./release/web_install.sh
			rm -rf ./release/appName
			mkdir -p ./release
			rm -rf dist/brand/index/*
			mkdir -p dist/brand/index
			cp -a ../../../web-server/brand/${brand}/${appName}/* dist/brand/index/
			tar -zcvf dist.tar.gz dist
			mv dist.tar.gz ./release/
			if [ -d "./debug" ]; then
				tar -zcvf debug.tar.gz debug
				mv debug.tar.gz ./release/
			fi
			echo "${appName}" >./release/appName
			\cp -av ../../../web-server/scripts/shell/web_install.sh ./release/
			makeself ./release ./${appName}_${brand}.bin "Deploy the ${appName}_${brand} application..." ./web_install.sh
			mkdir -p ../../../web-server/release_brand/
			rm -rf ../../../web-server/release_brand/${appName}_${brand}.bin
			mv ./${appName}_${brand}.bin ../../../web-server/release_brand/
		fi
	done
fi
cd ..
